home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 1 / Cream of the Crop 1.iso / PROGRAM / DJLGR106.ARJ / COLORS.C < prev    next >
C/C++ Source or Header  |  1992-04-08  |  3KB  |  137 lines

  1. /* This is file COLORS.C */
  2. /*
  3. ** Copyright (C) 1991 DJ Delorie, 24 Kirsten Ave, Rochester NH 03867-2954
  4. **
  5. ** This file is distributed under the terms listed in the document
  6. ** "copying.dj", available from DJ Delorie at the address above.
  7. ** A copy of "copying.dj" should accompany this file; if not, a copy
  8. ** should be available from where this file was obtained.  This file
  9. ** may not be distributed without a verbatim copy of "copying.dj".
  10. **
  11. ** This file is distributed WITHOUT ANY WARRANTY; without even the implied
  12. ** warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  13. */
  14.  
  15. #include "graphics.h"
  16.  
  17. extern int _GrCurMode;
  18.  
  19. typedef struct {
  20.   unsigned char n;
  21.   unsigned char state; /* 0=free, 1=shared, 2=writable */
  22.   unsigned char r;
  23.   unsigned char g;
  24.   unsigned char b;
  25. } Color;
  26.  
  27. static Color color[256];
  28.  
  29. static int initted=0;
  30.  
  31. static init_colormap()
  32. {
  33.   initted = 1;
  34.   bzero(color, sizeof(color));
  35.   GrAllocColor(0, 0, 0);
  36.   GrAllocColor(255, 255, 255);
  37. }
  38.  
  39. GrBlack()
  40. {
  41.   if (!initted)
  42.     init_colormap();
  43.   return 0;
  44. }
  45.  
  46. GrWhite()
  47. {
  48.   if (!initted)
  49.     init_colormap();
  50.   return 1;
  51. }
  52.  
  53. GrAllocColor(int r, int g, int b)
  54. {
  55.   int n;
  56.   if (!initted)
  57.     init_colormap();
  58.   for (n=0; n<256; n++)
  59.   {
  60.     if ((color[n].state == 1)
  61.       && (color[n].r == r)
  62.       && (color[n].g == g)
  63.       && (color[n].b == b))
  64.     {
  65.       color[n].n++;
  66.       return n;
  67.     }
  68.   }
  69.   for (n=0; n<256; n++)
  70.   {
  71.     if (color[n].state == 0)
  72.     {
  73.       color[n].n = 1;
  74.       color[n].state = 1;
  75.       GrSetColor(n, r, g, b);
  76.       return n;
  77.     }
  78.   }
  79. }
  80.  
  81. int GrAllocCell()
  82. {
  83.   int n;
  84.   if (!initted)
  85.     init_colormap();
  86.   for (n=0; n<256; n++)
  87.   {
  88.     if (color[n].state == 0)
  89.     {
  90.       color[n].n = 1;
  91.       color[n].state = 2;
  92.       return n;
  93.     }
  94.   }
  95. }
  96.  
  97. void GrSetColor(int n, int r, int g, int b)
  98. {
  99.   if (!initted)
  100.     init_colormap();
  101.   color[n].r = r;
  102.   color[n].g = g;
  103.   color[n].b = b;
  104.   _GrSetColor(n, r, g, b);
  105. }
  106.  
  107. void GrQueryColor(int n, int *r, int *g, int *b)
  108. {
  109.   if (!initted)
  110.     init_colormap();
  111.   *r = color[n].r;
  112.   *g = color[n].g;
  113.   *b = color[n].b;
  114. }
  115.  
  116. void GrFreeColor(int n)
  117. {
  118.   if (color[n].n > 0)
  119.   {
  120.     color[n].n --;
  121.     if (color[n].n == 0)
  122.       color[n].state = 0;
  123.   }
  124. }
  125.  
  126. void GrRefreshColors()
  127. {
  128.   int i;
  129.   if (_GrCurMode < GR_320_200_graphics)
  130.     return;
  131.   if (!initted)
  132.     init_colormap();
  133.   for (i=0; i<256; i++)
  134.     if (color[i].state)
  135.       GrSetColor(i, color[i].r, color[i].g, color[i].b);
  136. }
  137.